﻿/* Copyright 2008-10 Jarrett Vance
 * see http://www.codeplex.com/blogsvc/license */
namespace AtomSite.WebCore
{
  using System;
  using System.Collections.Generic;
  using System.Web.Mvc;
  using AtomSite.Domain;
  using StructureMap;
  using StructureMap.Attributes;

  public class AdminPlugin : BasePlugin
  {
    public static readonly string AdminAssetGroup = "admin";

    public AdminPlugin(ILogService logger)
      : base(logger)
    {
      DefaultMerit = (int)MeritLevel.High + 30;
      DefaultAssetGroup = AdminAssetGroup;
    }

    public override void Register(IContainer container, List<SiteRoute> routes, ViewEngineCollection viewEngines, ModelBinderDictionary modelBinders, ICollection<Asset> globalAssets)
    {
      container.Configure(a =>
      {
        a.ForRequestedType<AdminService>().CacheBy(InstanceScope.Singleton)
          .TheDefault.Is.OfConcreteType<AdminService>();

        a.ForRequestedType<IRouteService>().CacheBy(InstanceScope.HttpContext)
          .AddConcreteType<AdminRouteService>();

        //TODO: not sure why the above concrete type becomes the default so adding this line
        a.ForRequestedType<IRouteService>().CacheBy(InstanceScope.HttpContext)
          .TheDefault.Is.OfConcreteType<RouteService>();
      });

      RegisterController<AdminController>(container);

      //framework assets loaded in master page
      //RegisterFrameworkAsset(globalAssets, "jquery.js");
      //RegisterFrameworkAsset(globalAssets, "reset-fonts-grids-2.7.0.css");

      RegisterGlobalAsset(globalAssets, "jquery.timeago-0.8.1.js");
      RegisterGlobalAsset(globalAssets, "jquery.single-ddm-1.2.js");
      RegisterGlobalAsset(globalAssets, "jquery.tools-1.1.2.js");
      RegisterGlobalAsset(globalAssets, "Admin.js");
      RegisterGlobalAsset(globalAssets, "Admin.css");

      RegisterCompositeWidget(container, "AdminAcceptsWidget", "Admin", "Accepts",
        new[] { "AdminAcceptsWidget.css", "AdminAcceptsWidget.js" });

      RegisterPage(container, "AdminEntries", new[] { "AdminListings.css" });
      RegisterPage(container, "AdminAnnotations", new[] { "AdminListings.css" });

      RegisterViewWidget(container, "AdminAddCategoryWidget");

      RegisterCompositeWidget(container, "AdminCategoriesWidget", "Admin", "Categories",
        new[] { "AdminCategoriesWidget.css", "AdminCategoriesWidget.js" });

      RegisterViewWidget(container, "AdminCKEditorWidget", new[] { new Asset("ckeditor/ckeditor.js", Asset.NoGroup, AssetScope.Widget) }, string.Empty);

      RegisterViewWidget(container, "AdminCollectionsWidget", new[] { "AdminDefaultsToggle.js" });

      RegisterPage(container, "AdminEditEntry", new[] { "AdminEdit.css", "jquery.watermark.js", "AdminEdit.js" });
      RegisterPage(container, "AdminEditMedia", new[] { "AdminEdit.css", "jquery.watermark.js", "jquery.multifile-1.45.js", "AdminEdit.js" });

      RegisterCompositeWidget(container, "AdminPendingEntriesWidget", "Admin", "PendingEntries",
        new[] { "AdminItems.css" });

      RegisterPage(container, "AdminPlugins", new[] { "jquery.multifile-1.45.js", "AdminPlugins.js", "AdminPluginListing.css" });

      RegisterViewWidget(container, "AdminQuickPubWidget", new[] {"AdminQuickPubWidget.js", "AdminQuickPubWidget.css" });

      RegisterCompositeWidget(container, "AdminRecentAnnotationsWidget", "Admin", "RecentAnnotations",
        new[] { "AdminItems.css" });

      RegisterCompositeWidget(container, "AdminRightNowWidget", "Admin", "RightNow",
        new[] {"AdminListings.css", "AdminRightNowWidget.css" });

      RegisterPage(container, "AdminSettingsEntireSite", new[] { "AdminSettings.css" });
      RegisterPage(container, "AdminSettingsWorkspace", new[] { "AdminSettings.css" });
      RegisterPage(container, "AdminSettingsCollection", new[] { "AdminSettings.css" });

      RegisterPage(container, "AdminTheme", new[] { "AdminTheme.css", "jquery.rater-1.1.js", "AdminTheme.js", "RaterWidget.css" });

      RegisterViewWidget(container, "AdminTinyMceWidget"); //TODO: register assets
      
      RegisterViewWidget(container, "AdminQuickPubWidget", new[] { "AdminQuickPubWidget.css" });
      RegisterRoutes<AdminRouteRegistrar>(container, routes, typeof(AdminRouteService).AssemblyQualifiedName);

      RegisterViewWidget(container, "AdminWorkspacesWidget", new[] { "AdminDefaultsToggle.js" });

      RegisterCompositeWidget(container, "AdminAdministratorsWidget", "Admin", "Administrators",
        new[] { "AdminUsers.js" });

      RegisterCompositeWidget(container, "AdminPeopleWidget", "Admin", "People",
        new[] { "AdminUsers.js" });
      
    }

    public override PluginState Setup(IContainer container, string appPath)
    {
      SetupDashboard(container);
      SetupSettings(container);
      return base.Setup(container, appPath);
    }

    public override PluginState Upgrade(IContainer container, string appPath, Version previous, Version current)
    {
      if (previous == ServerApp.Version10) SetupDashboard(container);      
      if (previous == ServerApp.Version11) SetupSettings(container);

      return base.Upgrade(container, appPath, previous, current);
    }

    void SetupDashboard(IContainer container)
    {
      SetupIncludeInPageArea(container, "AdminDashboard", "dashboardLeft", "AdminRightNowWidget");
      SetupIncludeInPageArea(container, "AdminDashboard", "dashboardLeft", "AdminRecentAnnotationsWidget");
      SetupIncludeInPageArea(container, "AdminDashboard", "dashboardRight", "AdminQuickPubWidget");
      SetupIncludeInPageArea(container, "AdminDashboard", "dashboardRight", "AdminPendingEntriesWidget");
    }

    void SetupSettings(IContainer container)
    {
      SetupIncludeInPageArea(container, "AdminSettingsEntireSite", "settingsLeft", "AdminWorkspacesWidget");
      SetupIncludeInPageArea(container, "AdminSettingsEntireSite", "settingsRight", "AdminAdministratorsWidget");
      SetupIncludeInPageArea(container, "AdminSettingsWorkspace", "settingsLeft", "AdminCollectionsWidget");
      SetupIncludeInPageArea(container, "AdminSettingsWorkspace", "settingsRight", "AdminPeopleWidget");
      SetupIncludeInPageArea(container, "AdminSettingsCollection", "settingsLeft", "AdminAcceptsWidget");
      SetupIncludeInPageArea(container, "AdminSettingsCollection", "settingsMiddle", "AdminCategoriesWidget");
      SetupIncludeInPageArea(container, "AdminSettingsCollection", "settingsRight", "AdminPeopleWidget");
      SetupIncludeInPageArea(container, "AdminSettingsCollection", "settingsTabs", new LiteralInclude("<li><a href=\"#blogSettings\">Blog Settings</a></li>"));
      SetupIncludeInPageArea(container, "AdminSettingsCollection", "settingsPanes", "BlogSettingsWidget");
      SetupIncludeInPageArea(container, "AdminEditEntry", "editEntryContent", "AdminCKEditorWidget");
    }
  }
}
